package com.jzr.bedside.common;

import com.jzr.bedside.db1.service.TUserService;
import com.jzr.bedside.db1.vo.MyUserDetailsVo;
import com.jzr.bedside.util.SpringUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityHelper {
    public static MyUserDetailsVo getUserDetails(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUserDetailsVo userDetails = (MyUserDetailsVo)authentication.getPrincipal();
        return  userDetails;
    }
    public static Integer getUserId(){
        Integer userId = getUserDetails().getUserId();
        if(userId == null){
            TUserService userService = (TUserService) SpringUtil.getBean("TUserService");
            userId = userService.getIdByUserCode(getUserCode());
            assert userId != null;
        }
        return userId;
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        HttpSession session = request.getSession(false);
//        if(session != null) {
//            return (Integer) session.getAttribute("userId");
//        }else{
//            return -1;
//        }
    }
    public static String getUserCode(){
        return getUserDetails().getUsername();
    }
}
